﻿using System; 
using System.Data; 
using System.Configuration; 
using System.Collections; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
//
using Mmht.BT;
using System.IO;

public partial class admin_pdfgl_plscpdf: System.Web.UI.Page
{
    static public ArrayList hif = new ArrayList();
    //保存文件列表     
    public int filesUploaded = 0;
    //上传文件的数量  
    string message = string.Empty;
    int create_by;
    DateTime create_at;
    GetPdf gp = new GetPdf();
    public string tempPath;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["admin_id"] == null)
        {
            Response.Redirect("admin_logout.aspx");
            return;
        }
        try
        {
            create_by = (Int32)Session["admin_id"];
        }
        catch
        {
            create_by = 2;
        }
        
        if (!IsPostBack)
        {
            fileinsert();
        }

        tempPath = Server.MapPath("files/pdf/temp");
    }

    //-----------------添加列表  
    protected void AddFile_Click(object sender, EventArgs e)
    {
        if (Page.IsPostBack == true)
        //这个很重要哦．呵呵．按键在第一次ＰＡＧＥ加载后才起作用        
        {
            
            fileinsert();
        }
        else { }
    } 
    
    ///从listbox中删除指定的文件 
    protected void DelFile_Click(object sender, EventArgs e)
    {
        if (FileList.SelectedIndex == -1)  //if the Listbox's item is not selected  
        {
            message = "错误 - 必须指定要删除的文件.";
            message_show();
            return; 
        }
        else if (FileList.Items.Count != 0)
        {
            try
            {
                hif.RemoveAt(FileList.SelectedIndex);
                //Remove the item which is selected from the ArryList                                 
            }
            catch
            {

            }
            FileList.Items.Remove(FileList.SelectedItem.Text);
            //Remove the item which is selected in the listbox.     
        }
    }  
   
    /// <summary>    
    /// 
    /// 循环上传listbox中的文件到指定的文件夹下     
    /// </summary>    
    ///
    /// <param name="sender"></param>     
    /// <param name="e"></param>  
    protected void Upload_Click(object sender, EventArgs e)
    {
        copy();
    }

    private void message_show()
    {
        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script language='javascript'>  warning('" + message + "');</script>");
    }
    /// <summary>
    /// 为新上传的pdf生成数据库数据和缩略图
    /// </summary>
    /// <param name="dt"></param>
    /// <param name="type"></param>
    /// <param name="createby"></param>
    /// <param name="createat"></param>
    private void pdf_upload(DataTable dt, string type, int createby, DateTime createat)
    {
        gp.UploadPdf(dt, type, createby, createat);
        //同时生成缩略图
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow row = dt.Rows[i];
            string pdfName = row["name_cn"].ToString();
            ImagePdf ip = new ImagePdf();
            ip.CreatePdfImageByName(pdfName, type);
        }
    }

    private void data_show()
    {
        DataTable dt = new DataTable();
        dt = gp.UploadPdfInfo(create_at, create_by);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

   

    private void delete_file(string content, string type)
    {
        if (Cb_Del.Checked)
        {
            FileDelete fd = new FileDelete(content, type);
            fd.file_delete();
            Cb_Del.Checked = false;
        }
    }

    private void copy()
    {
        string dirp = Server.MapPath("files/pdf/temp/");
        DirectoryInfo mydir = new DirectoryInfo(dirp);

        string url0 = "files/pdf/" + DropDownList1.SelectedItem.Value + "/";
        //文件路径//files/pdf/bz/GB_1221-1992耐热钢棒.pdf
        string type = DropDownList1.SelectedItem.Text;//文件类型
        create_at = DateTime.Now;
        DataTable dt = new DataTable();
        dt.Columns.Add("name_cn");
        dt.Columns.Add("url");
       
        string LocalPath = Server.MapPath(url0); // 上传路径  

        string message2 = "临时文件夹E:/mmht/Mmht/files/pdf/temp内：";
        foreach (FileSystemInfo fsi in mydir.GetFileSystemInfos())
        {
            try
            {
                if (fsi is FileInfo)
                {
                    FileInfo fi = (FileInfo)fsi;
                    string x = System.IO.Path.GetDirectoryName(fi.FullName);
                    Console.WriteLine(x);
                    string s = System.IO.Path.GetExtension(fi.FullName);
                    string y = System.IO.Path.GetFileNameWithoutExtension(fi.FullName);
                    string fn = string.Empty;
                    string fn0 = y + s;
                    fn = fn0.Replace(" ", "");
                    if (s == ".pdf")
                    {
                        try
                        {
                            System.IO.File.Copy(fi.FullName, LocalPath + @"\" + fn);
                            filesUploaded++;
                            FileListupload.Items.Add(fn);
                            FileList.Items.Remove(fn0);
                            //拷贝源文件至上传路径  
                        }
                        catch
                        {
                            message += fn + "文件已存在，覆盖源文件。";
                            try
                            {
                                System.IO.File.Delete(LocalPath + @"\" + fn);
                                System.IO.File.Copy(fi.FullName, LocalPath + @"\" + fn);
                                filesUploaded++;
                                FileListupload.Items.Add(fn);
                                FileList.Items.Remove(fn0);
                            }
                            catch
                            {
                                message += fn + "文件无法上传，请检查文件是否损坏。";
                            }
                        }
                        try
                        {
                            System.IO.File.Delete(fi.FullName);
                            //删除源文件
                        }
                        catch
                        {
                            message2 += fn0 + "、";
                        }
                        string name_cn = fn.Substring(0, fn.Length - 4);
                        string url = url0 + fn;

                        DataRow dr = dt.NewRow();
                        dr["name_cn"] = name_cn;
                        dr["url"] = url;
                        dt.Rows.Add(dr);

                        //HIF.PostedFile.SaveAs(LocalPath + fn);
                        //filesUploaded++;
                        //FileListupload.Items.Add(fn);
                        //FileList.Items.Remove(fn0);
                    }
                }
            }
            catch (Exception err)
            {
                message += "上传错误 " + LocalPath + "" + err.ToString() + ".";
                //message_show();
            }
        }
        message += "共上传成功了 " + filesUploaded + " 个pdf文件.";
        if (message2.Contains("、"))
        {
            message += "文件：" + message2.Substring(0, message2.Length - 1) + "文件无法删除，如有需要请手动删除！";
        }
        message_show();
        hif.Clear();
        //FileList.Items.Clear();
        pdf_upload(dt, type, create_by, create_at);
        data_show();
    }

    private void fileinsert()
    {
        string dirp = Server.MapPath("files/pdf/temp/");
        DirectoryInfo mydir = new DirectoryInfo(dirp);

        foreach (FileSystemInfo fsi in mydir.GetFileSystemInfos())
        {
            if (fsi is FileInfo)
            {
                FileInfo fi = (FileInfo)fsi;
                string x = System.IO.Path.GetDirectoryName(fi.FullName);
                Console.WriteLine(x);
                string s = System.IO.Path.GetExtension(fi.FullName);
                string y = System.IO.Path.GetFileNameWithoutExtension(fi.FullName);
                string fn = string.Empty;
                string fn0 = y + s;

                FileList.Items.Add(fn0);

            }
            
        }
    }
    
}
